using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.MetadirectoryServices;

namespace ILMXFrameWork.XMA.Trace
{
    /// <summary>
    /// The TraceLog Class append lines to a file for debugging.
    /// </summary>
    /*Copyright (c) 2009, Joseph Stepongzi
    All rights reserved.
    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of Joseph Stepongzi nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
     */
    public class TraceLog
    {
        //Variables
        System.IO.StreamWriter sw = null;
        bool enableTracing = false;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="filePath">pass the filename variable to get the location for the trace file.</param>
        /// <param name="configParameters">pass the configParameters to see if trace loggin is enabled.</param>

        public TraceLog(string filePath, ConfigParameterCollection configParameters)
        {
            string strEnableTracing = configParameters["Enable Tracing"].Value.ToString();
            if (strEnableTracing.ToLower() == "false" || strEnableTracing.ToLower() == "true")
            {
                enableTracing = Convert.ToBoolean(configParameters["Enable Tracing"].Value.ToString());
            }
            else
            {
                throw new UnexpectedDataException("Malformed Enable Tracing Parameter in MA Config, it needs to be a boolean value of true or false");
            }
            if (enableTracing)
            {
                string[] splitfullImportPath = filePath.Split('\\');
                string splitfullImportFile = splitfullImportPath[splitfullImportPath.Length - 1];
                filePath = filePath.Replace(splitfullImportFile, "TraceLog.txt");
                this.sw = new StreamWriter(filePath);
            }
        }
        /// <summary>
        /// Check if Trace Logging has been enabled.
        /// </summary>
        public bool TraceEnabled()
        {
            return enableTracing;
        }
        /// <summary>
        /// Writes Line to Trace File.
        /// </summary>
        public void WriteLine(string line)
        {
            sw.WriteLine(line);
        }
        /// <summary>
        /// Close the file.
        /// </summary>
        public void Close()
        {
            sw.Close();
        }   
    }
}
